// pages/photos/photos.js
import {request} from '../../utils/request'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    downUrl:"",
    ablumId:"",
    page: {
      total: 0,
      current: 1,
      size: 20,
      isAsc: false
    },
    photos:[],
    check:false,
    ablumName:'',
    bigPhotos:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      ablumId:options.id,
      check :  options.check == 'true'? true : false,
      ablumName:options.ablum
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      downUrl: app.globalData.url + '/daily/photo/photo/down?path='
    })
    this.getPhotos()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.photos = []
    this.page.current = 1
    this.getPhotos()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.photos.length/this.data.page.size < 1){
       return
    }
    this.setData({
      'page.current' : this.data.page.current+ 1
    })
    this.getPhotos();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getPhotos(){
    request({
      url:'/daily/photo/album/detail',
      method:'get',
      data: {"current":this.data.page.current,"size":this.data.page.size,"albumId":this.data.ablumId},
      headers:{
        "Content-Type":"application/x-www-form-urlencoded"
      }
    }).then(res =>{
      if(res.code === 0){
          this.setData({
            photos:this.data.photos.concat(res.data.records)
          })
      }else{
        wx.showToast({
          title: res.msg,
          duration: 1000
        });
      }
    })
  },
  add_photo(){
    wx.navigateTo({
      url: '/pages/addPhoto/addPhoto?id=' + this.data.ablumId + '&ablum=' + this.data.ablumName ,
    })
  },
  showBig(e){

    this.data.bigPhotos = []
  
    for(let i = 0 ; i < this.data.photos.length; i++){
      this.data.bigPhotos.push(this.data.downUrl + this.data.photos[i].bigImgUrl)
    }
    wx.previewImage({
      current: this.data.downUrl + e.currentTarget.dataset.big , // 当前显示图片的http链接
      urls: this.data.bigPhotos // 需要预览的图片http链接列表
    })
  }
})